package com.geek.jdbc.work2v2.service;



import com.geek.jdbc.work2v2.dao.CustomerDao;
import com.geek.jdbc.work2v2.entity.Customer;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;


public class CustomerTest {
    private static CustomerDao sd =  new CustomerDao();

    public static void main(String[] args) {
//        Scanner sc = new Scanner(System.in);
//        System.out.println("请输入用户名：");
//        String name = sc.nextLine();
//        System.out.println("请输入密码：");
//        String pwd = sc.nextLine();
        try {
//            Customer user = sd.selectStudentByName(name);
//            if (user == null) {
//                System.out.println("用户名不存在！");
//                return;
//            }
//            if (!pwd.equals("8888")){
//                System.out.println("密码错误！");
//                return;
//            }
//            System.out.println("登陆成功！");
            Login();
        } catch (SQLException | ParseException e) {
//            System.out.println("SQL出现问题！");
            e.printStackTrace();
        }
    }

    private static void Login() throws SQLException, ParseException {
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("================请选择操作键======================");
            System.out.println("1. 插入客户信息");
            System.out.println("2. 修改客户名字");
            System.out.println("3. 查看客户名单");
            System.out.println("4. 按编号查询客户信息");
            System.out.println("5. 按姓名查询客户信息");
            System.out.println("6. 删除客户记录");
            System.out.println("0. 退出");

            System.out.println("请输入你要进行的操作：");
            int index = sc.nextInt();
            switch (index) {
                case 1:
                    Task1();
                    break;
                case 2:
                    Task2();
                    break;
                case 3:
                    Task3();
                    break;
                case 4:
                    Task4();
                    break;
                case 5:
                    Task5();
                    break;
                case 6:
                    Task6();
                    break;
                case 0:
                    System.out.println("感谢使用！");
                    return;
                default:
                    System.out.println("输入错误！");
            }
        }
    }

//    //1. 统计学生人数
//    public static void Task1() throws SQLException {
//        System.out.println("操作键：1");
//        List<Customer> list = sd.selectAllList();
//        System.out.println("所有学生人数为：" + list.size());
//    }

    //1. 插入客户信息
    public static void Task1() throws SQLException, ParseException {
        System.out.println("================插入操作======================");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要插入的客户的编号：");
        String id = sc.nextLine();
        System.out.println("请输入要插入的客户的姓名：");
        String name = sc.nextLine();
        System.out.println("请输入要插入的客户的邮箱：");
        String email = sc.nextLine();
        System.out.println("请输入要插入的客户的生日(要求格式为YYYY-MM-DD)：");
        String brithday = sc.nextLine();

        if (id.isEmpty() || name.isEmpty() || email.isEmpty() || brithday.isEmpty()){
            System.out.println("不能有一项数据为空！");
            return;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(brithday);
        Customer s = new Customer(id, name, email, date);
        int i = sd.insertStudent(s);
        if (i > 0){
            System.out.println("插入成功！");
            return;
        }
        System.out.println("插入失败！");
    }

    //2. 修改客户姓名
    public static void Task2() throws SQLException {
        System.out.println("================修改操作======================");
        System.out.print("请输入待修改的客户的编号：");
        Scanner sc = new Scanner(System.in);
        String id = sc.nextLine();
        System.out.println();
        System.out.print("请输入修改后的名字：");
        String name = sc.nextLine();
        if (sd.updateByName(id, name)){
            System.out.println("修改成功！");
            return;
        }
        System.out.println("修改失败！");
    }

    //3. 查看客户名单
    public static void Task3() throws SQLException {
        System.out.println("================查看操作======================");
        List<Customer> list = sd.selectAllList();
        if (list.isEmpty()){
            System.out.println("查看失败！");
            return;
        }
        System.out.println("----------------客户列表----------------------");
        System.out.println("编号\t\t姓名\t\t\t邮箱\t\t\t生日");
        for (Customer stu : list) {
            System.out.println(stu.getId() + "\t\t" + stu.getName() + "\t\t" +
                    stu.getEmail() + "\t\t" + stu.getBrithday());
        }
    }

    //4. 按编号查询客户信息
    public static void Task4() throws SQLException {
        System.out.println("================查询操作======================");
        System.out.print("请输入客户编号：");
        Scanner sc = new Scanner(System.in);
        String id = sc.nextLine();

        Customer s = sd.selectStudentById(id);
        if (s != null) {
            System.out.println("查询成功！");
            System.out.println("编号\t\t姓名\t\t\t邮箱\t\t\t生日");
            System.out.println(s.getId() + "\t\t" + s.getName() +
                    "\t\t" + s.getEmail() + "\t\t" + s.getBrithday());
        }
    }

    //5. 按姓名查询学生信息
    public static void Task5() throws SQLException {
        System.out.println("================查询操作======================");
        System.out.print("请输入客户姓名：");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        List<Customer> list = sd.selectAllStudent(name);
        System.out.println("学号" + "\t\t" + "姓名" + "\t\t" + "邮箱" + "\t\t" + "出生日期");
        if (list.isEmpty()){
            System.out.println("查无此人！");
            return;
        }
        for (Customer stu : list) {
            System.out.println(stu.getId() + "\t\t" + stu.getName() +
                    "\t\t" + stu.getEmail() + "\t\t" + stu.getBrithday());
        }
    }



    //6. 删除学生记录
    public static void Task6() throws SQLException {
        System.out.println("================删除操作======================");
        System.out.print("请输入客户编号：");
        Scanner sc = new Scanner(System.in);
        String id = sc.nextLine();
        Customer s = sd.selectStudentById(id);
        if (s == null){
            System.out.println("查无此人！");
            return;
        }
        System.out.println("确认要删除编号为" + s.getId() + "，姓名为" + s.getName() + "的客户信息吗？(Y/N)");
        String answer = sc.nextLine();
        switch (answer){
            case "Y":
                if (sd.delete(id)) {
                    System.out.println("删除成功！");
                }else {
                    System.out.println("删除失败！");
                }
                break;
            case "N":
                System.out.println("删除失败！");
                break;
            default:
                System.out.println("输入错误！");
                break;
        }
    }


}
